package com.xiaojian.pick.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaojian.pick.entity.TopicSupport;
import com.xiaojian.pick.mapper.TopicSupportMapper;
import com.xiaojian.pick.service.TopicService;
import com.xiaojian.pick.service.TopicSupportService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author whycode
 * @title: CommentSupportServiceImpl
 * @projectName campus-secondary-market-app---sys
 * @description: TODO
 * @date 2022/4/916:13
 */

@Service
public class TopicSupportServiceImpl extends ServiceImpl<TopicSupportMapper, TopicSupport> implements TopicSupportService {

    @Resource
    @Lazy
    private TopicService topicService;

    @Override
    public List<TopicSupport> queryByTopic(Integer topicId) {
        return getBaseMapper().queryByTopic(topicId);
    }

    @Override
    @Transactional
    public boolean save(TopicSupport entity) {
        boolean save = super.save(entity);
        if(save){
            topicService.modifySupportCount(entity.getTopicId(), 1);
        }
        return save;
    }

    @Override
    @Transactional
    public boolean delete(TopicSupport topicSupport) {
        boolean remove = this.remove(new QueryWrapper<TopicSupport>().eq("topic_id", topicSupport.getTopicId())
                .eq("user_id", topicSupport.getUserId()));
        if(remove){
            topicService.modifySupportCount(topicSupport.getTopicId(), -1);
        }
        return remove;
    }
}
